//
//  SunGesSetVC.m
//  Xspcf
//
//  Created by sunshine on 16/11/17.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunGesSetVC.h"
#import "PCCircleView.h"
#import "PCCircleViewConst.h"
#import "PCLockLabel.h"
#import "PCCircleInfoView.h"
#import "PCCircle.h"

@interface SunGesSetVC ()<CircleViewDelegate>
{
    UIBarButtonItem *_resignBtn;
    PCCircleView *_lockView;
    PCLockLabel *_msgLabel;
    PCCircleInfoView *_infoView;
}
@end

@implementation SunGesSetVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置手势密码";
    self.view.backgroundColor = WHITE;
    [self creatUI];
    [PCCircleViewConst saveGesture:nil Key:gestureOneSaveKey];
}

- (BOOL)navigationShouldPopOnBackButton{
    [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
    return NO;
}

- (void)creatUI{
    _resignBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(resign)];
    _resignBtn.tintColor = WHITE;
    self.navigationItem.rightBarButtonItem = nil;

    _infoView = [[PCCircleInfoView alloc] init];
    [self.view addSubview:_infoView];
    [_infoView lt_setTop:Y(58) to:nil save:YES];
    [_infoView lt_setSize:CGSizeMake(X(40), X(40)) save:YES];
    [_infoView lt_XcenterTo:nil save:YES];

    _msgLabel = [[PCLockLabel alloc] init];
    [self.view addSubview:_msgLabel];
    [_msgLabel lt_setTop:Y(24) to:_infoView save:YES];
    [_msgLabel lt_XcenterTo:nil save:YES];
    [_msgLabel showNormalMsg:gestureTextBeforeSet];
    
    _lockView = [[PCCircleView alloc] init];
    [self.view addSubview:_lockView];
    _lockView.delegate = self;
    [_lockView lt_setTop:Y(40) to:_msgLabel save:YES];
    [_lockView lt_setSize:CGSizeMake(X(270), X(270)) save:YES];
    [_lockView lt_XcenterTo:nil save:YES];
    _lockView.type = CircleViewTypeSetting;
}

- (void)resign{
    self.navigationItem.rightBarButtonItem = nil;
    [self infoViewDeselectedSubviews];
    [_msgLabel showNormalMsg:gestureTextBeforeSet];
    [PCCircleViewConst saveGesture:nil Key:gestureOneSaveKey];
}

#pragma mark - circleView - delegate
- (void)circleView:(PCCircleView *)view type:(CircleViewType)type connectCirclesLessThanNeedWithGesture:(NSString *)gesture
{
    NSString *gestureOne = [PCCircleViewConst getGestureWithKey:gestureOneSaveKey];
    // 看是否存在第一个密码
    if ([gestureOne length]) {
        self.navigationItem.rightBarButtonItem = _resignBtn;
        [_msgLabel showWarnMsgAndShake:gestureTextDrawAgainError];
    } else {
        [_msgLabel showWarnMsgAndShake:gestureTextConnectLess];
    }
}

- (void)circleView:(PCCircleView *)view type:(CircleViewType)type didCompleteSetFirstGesture:(NSString *)gesture{
    [_msgLabel showWarnMsg:gestureTextDrawAgain];
    // infoView展示对应选中的圆
    [self infoViewSelectedSubviewsSameAsCircleView:view];
}

- (void)circleView:(PCCircleView *)view type:(CircleViewType)type didCompleteSetSecondGesture:(NSString *)gesture result:(BOOL)equal{
    //NSLog(@"获得第二个手势密码%@",gesture);
    if (equal) {
//        NSLog(@"两次手势匹配！可以进行本地化保存了");
        [_msgLabel showWarnMsg:gestureTextSetSuccess];
        [PCCircleViewConst saveGesture:gesture Key:gestureFinalSaveKey];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"IsSetGesture"];
        [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
    } else {
        [_msgLabel showWarnMsgAndShake:gestureTextDrawAgainError];
        self.navigationItem.rightBarButtonItem = _resignBtn;
    }
}

#pragma mark - 让infoView对应按钮选中
- (void)infoViewSelectedSubviewsSameAsCircleView:(PCCircleView *)circleView
{
    for (PCCircle *circle in circleView.subviews) {
        
        if (circle.state == CircleStateSelected || circle.state == CircleStateLastOneSelected) {
            
            for (PCCircle *infoCircle in _infoView.subviews) {
                if (infoCircle.tag == circle.tag) {
                    [infoCircle setState:CircleStateSelected];
                }
            }
        }
    }
}

#pragma mark - 让infoView对应按钮取消选中
- (void)infoViewDeselectedSubviews
{
    [_infoView.subviews enumerateObjectsUsingBlock:^(PCCircle *obj, NSUInteger idx, BOOL *stop) {
        [obj setState:CircleStateNormal];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
